/**
 *  Copyright 2010 ShenYang voole Co. Ltd.
 *  All right reserved. 
 */
package cn.piaoyi.dao.hibernate;


import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.piaoyi.bean.Admin;
import cn.piaoyi.bean.OrderInfo;
import cn.piaoyi.bean.UserInfo;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.dao.AdminDAO;
import cn.piaoyi.dao.OrderInfoDAO;
import cn.piaoyi.dao.UserInfoDAO;

/**
 * 功能描述:订单操作实现
 *
 * 创建日期 Apr 10, 2010
 *
 * @author zhangbaochao
 *
 * @version 1.0
 *
 */
public class OrderInfoHibernateDAO extends HibernateDaoSupport implements OrderInfoDAO {

	private static final String LOAD_BY_ID = "from OrderInfo where id = ?";
	private static final String LOAD_BY_NO = "from OrderInfo where orderNO = ?";
	private static final String  REMOVE_BY_ID = "delete from OrderInfo where id = ?";
	
	/**
	 * 保存OrderInfo对象
	 * 
	 * @param orderInfo
	 * 			OrderInfo对象
	 * @return OrderInfo
	 * 			OrderInfo对象
	 */
	public OrderInfo saveOrderInfo(OrderInfo orderInfo){
		this.getHibernateTemplate().save(orderInfo);
		return orderInfo;
	}
	/**
	 * 保存或更新OrderInfo对象
	 * 
	 * @param orderInfo
	 * 			OrderInfo对象
	 * @return OrderInfo
	 * 			OrderInfo对象
	 */
	public OrderInfo saveOrUpdateOrderInfo(OrderInfo orderInfo){
		this.getHibernateTemplate().saveOrUpdate(orderInfo);
		return orderInfo;
	}
	/**
	 * 保存OrderInfo对象
	 * 
	 * @param orderInfo
	 * 			OrderInfo对象
	 * @return OrderInfo
	 * 			OrderInfo对象
	 */
	public OrderInfo updateOrderInfo(OrderInfo orderInfo){
		this.getHibernateTemplate().update(orderInfo);
		return orderInfo;
	}
	
	/**
	 * 根据主键ID获取OrderInfo对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return OrderInfo
	 * 			OrderInfo对象
	 */
	public OrderInfo findOrderInfoById(long id){
		List l = this.getHibernateTemplate().find(LOAD_BY_ID,id);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (OrderInfo) l.get(0);
		}
	}
	
	/**
	 * 根据主键orderNO获取OrderInfo对象
	 * 
	 * @param String
	 * 			订单号
	 * @return OrderInfo
	 * 			OrderInfo对象
	 */
	public OrderInfo findOrderInfoByOrderNO(String orderNO){
		List l = this.getHibernateTemplate().find(LOAD_BY_NO,orderNO);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (OrderInfo) l.get(0);
		}
	}
	
	/**
	 * 获取OrderInfo对象个数
	 * 
	 * @param userId
	 * 			long
	 * @param orderStatus
	 * 			int
	 * @param shippingStatus
	 * 			int
	 * @param payStatus
	 * 			int
	 * @return long
	 * 			OrderInfo对象个数
	 */
	public long getOrderInfoNum(final long userId,final int orderStatus,final int shippingStatus,final int payStatus){
		List l = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(OrderInfo.class);
				c.setProjection(Projections.count("id"));
				if (userId != -1) {
					c.add(Restrictions.eq("userId", new Long(userId)));
				}
				if (orderStatus != -1) {
					c.add(Restrictions.eq("orderStatus", new Integer(orderStatus)));
				}
				if (shippingStatus != -1) {
					c.add(Restrictions.eq("shippingStatus", new Integer(shippingStatus)));
				}
				if (payStatus != -1) {
					c.add(Restrictions.eq("payStatus", new Integer(payStatus)));
				}
				return c.list();
			}
		});
		if (l == null || l.isEmpty()) {
			return 0;
		} else {
			return ((Integer) l.get(0)).longValue();
		}
	}
	
	/**
	 * 获取OrderInfo列表
	 * 
	 * @param userId
	 * 			long
	 * @param orderStatus
	 * 			int
	 * @param shippingStatus
	 * 			int
	 * @param payStatus
	 * 			int
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @param firstResult
	 *            int
	 * @param maxResults
	 *            int
	 * @return List
	 * 			OrderInfo对象列表
	 */
	public List findOrderInfos(final long userId,final int orderStatus,final int shippingStatus,final int payStatus,final String orderby, final int ascOrDesc, final int firstResult, final int maxResults){
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(OrderInfo.class);
				if (userId != -1) {
					c.add(Restrictions.eq("userId", new Long(userId)));
				}
				if (orderStatus != -1) {
					c.add(Restrictions.eq("orderStatus", new Integer(orderStatus)));
				}
				if (shippingStatus != -1) {
					c.add(Restrictions.eq("shippingStatus", new Integer(shippingStatus)));
				}
				if (payStatus != -1) {
					c.add(Restrictions.eq("payStatus", new Integer(payStatus)));
				}
				if (StringUtils.isNotBlank(orderby)) {
					if (ascOrDesc == Constant.ORDER_ASC) {
						c.addOrder(Order.asc(orderby));
					}
					if (ascOrDesc == Constant.ORDER_DESC) {
						c.addOrder(Order.desc(orderby));
					}
				}
				c.setFirstResult(firstResult);
				c.setMaxResults(maxResults);
				return c.list();
			}
		});
	}
	
	
	/**
	 * 删除OrderInfo对象
	 */
	public void removeOrderInfoById(final long id){
	    getHibernateTemplate().execute(new HibernateCallback() {
	        public Object doInHibernate(Session s) throws HibernateException, SQLException {
	          Query query = s.createQuery(REMOVE_BY_ID);
	          query.setLong(0, id);
	          query.executeUpdate();
	          return null;
	        }
	      });
	}
}
